package net.oschina.app.ui;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import net.oschina.app.Myapp;
import net.oschina.app.R;
import net.oschina.app.hjpbean.BlogBean;
import net.oschina.app.hjpbean.FansBean;
import net.oschina.app.hjpbean.GeneralBlogDetailBean;
import net.oschina.app.hjpbean.GeneralNewsBean;
import net.oschina.app.hjpbean.GeneralNewsDetailBean;
import net.oschina.app.hjpbean.LoginBean;
import net.oschina.app.hjpbean.UserBean;
import net.oschina.app.hjpbean.findbean.FriendBean;
import net.oschina.app.util.LogUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class TestActivity extends AppCompatActivity {

    @BindView(R.id.bt)
    Button bt;
    @BindView(R.id.lv)
    ListView lv;
    @BindView(R.id.use)
    EditText use;
    @BindView(R.id.pwd)
    EditText pwd;
    @BindView(R.id.bt2)
    Button bt2;
    private List<GeneralNewsBean.NewsBean> newslist;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
        ButterKnife.bind(this);


        Call<GeneralBlogDetailBean> getboke = Myapp.shopService.getboke(470948);

        getboke.enqueue(new Callback<GeneralBlogDetailBean>() {
            @Override
            public void onResponse(Call<GeneralBlogDetailBean> call, Response<GeneralBlogDetailBean> response) {
                BlogBean blog = response.body().getBlog();
                String title = blog.getTitle();

                LogUtils.d(title);


            }

            @Override
            public void onFailure(Call<GeneralBlogDetailBean> call, Throwable t) {
                t.printStackTrace();
            }
        });




        Call<GeneralNewsDetailBean> getzixi = Myapp.shopService.getzixi(64198);

        getzixi.enqueue(new Callback<GeneralNewsDetailBean>() {
            @Override
            public void onResponse(Call<GeneralNewsDetailBean> call, Response<GeneralNewsDetailBean> response) {
                GeneralNewsDetailBean.NewsBean news = response.body().getNews();
                String body = news.getBody();
                LogUtils.d(body);
            }

            @Override
            public void onFailure(Call<GeneralNewsDetailBean> call, Throwable t) {
                t.printStackTrace();
            }
        });

        //        iniData();
//        iniEnvent();


    }

    private void iniEnvent() {

        String use1 = use.getText().toString();
        String pwd1 = pwd.getText().toString();

        bt2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Call<LoginBean> hotTweetInfo = Myapp.shopService.getLogin(1, "805048523@qq.com", "zhong5guo");//发现 活动  近期活动  成功
                hotTweetInfo.enqueue(new Callback<LoginBean>() {
                    @Override
                    public void onResponse(Call<LoginBean> call, Response<LoginBean> response) {
                        LoginBean.RusultBean result = response.body().getResult();
                        String errorCode = result.getErrorCode();
                        String errorMessage = result.getErrorMessage();
                        LogUtils.d(errorMessage);

                        UserBean user = response.body().getUser();


                        LogUtils.d(user.getName());

                        Toast.makeText(TestActivity.this, "登录结果!: " + errorMessage, Toast.LENGTH_SHORT).show();
                      }

                    @Override
                    public void onFailure(Call<LoginBean> call, Throwable t) {
                        t.printStackTrace();
                    }
                });


            }
        });


    }


    private void iniData() {

        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //具体业务  不用封装
                //                Call<GeneralNewsBean> info = Myapp.shopService.getHotInfo(0,  4,"week", 20);//获得热点信息

                //综合的 博客 成功     Call<GeneralBlogBean> week = Myapp.shopService.getBlogInfo(0, 20, "latest");

                //综合的 推荐 成功     Call<GeneralBlogBean> week = Myapp.shopService.getRecommendInfo(0, 20, "recommend");

                //动弹 最新动弹   成功  Call<TweetsList> week = Myapp.shopService.getNewTweetInfo(0, 0, 20);

                //  动弹 热门动弹   成功         Call<TweetsList> hotTweetInfo = Myapp.shopService.getHotTweetInfo(-1, 0, 20);

                //我的动弹 没有测试 需要登录            Call<TweetsList> hotTweetInfo = Myapp.shopService.getMyTweetInfo(-1, 0, 20);


//                http://www.oschina.net/action/api/friends_list?uid=2911324&pageIndex=0&pageSize=20&relation=0

                Call<FansBean> fansBeanCall = Myapp.shopService.getfriendsInfo(3611588, 0, 20, 0);//发现 活动  近期活动  成功

                fansBeanCall.enqueue(new Callback<FansBean>() {


                    @Override
                    public void onResponse(Call<FansBean> call, Response<FansBean> response) {
                        List<FriendBean> friends = response.body().getFriends();


                         My my = new My(friends);
                        lv.setAdapter(my);


                    }

                    @Override
                    public void onFailure(Call<FansBean> call, Throwable t) {
                        t.printStackTrace();
                    }
                });


            }
        });
    }


    class My extends BaseAdapter {


        private List<FriendBean> newslist;

        public My(List<FriendBean> newslist) {
            this.newslist = newslist;
        }

        @Override
        public int getCount() {
            return newslist == null ? 0 : newslist.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                convertView = View.inflate(TestActivity.this, R.layout.hjp_lv_item, null);
                holder = new ViewHolder(convertView);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            FriendBean friendBean = newslist.get(position);
            //            String name = userBean.getName();
            //            holder.tvTitle.setText(name);
             holder.tvTitle.setText(friendBean.getName());


            return convertView;
        }

    }


}

class ViewHolder {
    @BindView(R.id.tv_Title)
    TextView tvTitle;
    @BindView(R.id.tv_msg)
    TextView tvMsg;

    ViewHolder(View view) {
        ButterKnife.bind(this, view);
    }
}